package com.lnsf.bookstore.system.permission.service.impl;
;
import com.lnsf.bookstore.system.permission.service.PermissionService;
import com.lnsf.bookstore.system.permission.vo.Permission;
import com.lnsf.bookstore.common.constant.Constants;
import com.lnsf.bookstore.system.role.dao.RolePermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lnsf.bookstore.system.permission.dao.PermissionMapper;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements PermissionService {

	@Autowired
	private PermissionMapper permissionMapper;

	@Autowired
	private RolePermissionMapper rolePermissionMapper;

	@Override
	public void deleteBatch(Integer[] permissionIds) {
		permissionMapper.deleteBatch(permissionIds);
	}

	@Override
	public Permission findById(Integer id) {
		System.out.println("findById ==> id:"+id);
		return permissionMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(Permission permission) {
		return permissionMapper.insertSelective(permission);
	}

	@Override
	public int update(Permission permission) {
		return 	permissionMapper.updateByPrimaryKeySelective(permission);

	}

	@Override
	public int deletebyId(Integer permissionId) {
		deleteRolePermByPermId(permissionId);//先删除中间表
		List<Integer> permList = new ArrayList<Integer>();
		permList.add(permissionId);

		List<Integer> list = permissionMapper.listChildrenIdByPid(permList);
		if(list.size()>0){
			List<Integer> btnIds = permissionMapper.listChildrenIdByPid(list);
			if(btnIds.size()>0){
				list.addAll(btnIds);
			}
			//执行删除
			Example example = new Example(Permission.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andIn("id", list);
			permissionMapper.deleteByExample(example);
		}
	  return 	permissionMapper.deleteByPrimaryKey(permissionId); //删除自己

	}

	/**
	 * 对应的用户，能查看的权限菜单
	 * @param userId
	 * @return
	 */
	@Override
	public List<Permission> queryPermbyUserId(Integer userId) {
		List<Permission> list = permissionMapper.listPermissionByUserId(userId,0);
		if(list.size()>0){
			for (Permission permission : list){
				List<Permission> childList = permissionMapper.listPermissionByUserId(userId,permission.getId());
				permission.setChildren(childList);
			}
		}
		return list;
	}

	/**
	 * 超级管理员，全部显示权限菜单
	 * @return
	 */
	@Override
	public List<Permission> listAllExBtn() {
		List<Integer> status = new ArrayList<Integer>();
		status.add(Constants.ONE);
		//后面需要改为根据用户的角色Id查询
		List<Permission> list = listByPid(0, status);//查询一级菜单
		if (list.size() > 0) {
			for (Permission permission : list) {
				List<Permission> childrens = listByPid(permission.getId(), status);//二级菜单
				permission.setChildren(childrens);//一级菜单中保存二级菜单
			}
		}
		return list;
	}

	@Override
	public List<Permission> queryNotButtonList() {
		Example example = new Example(Permission.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andNotEqualTo("type",2);
		criteria.andEqualTo("status",1);
		example.setOrderByClause("sort");
		return permissionMapper.selectByExample(example);
	}

	/**
	 * 查询全部，嵌套查询
	 */
	/*@Override
	public List<Permission> listAll() {
		List<Integer> status = new ArrayList<Integer>();
		status.add(Constants.ZERO);
		status.add(Constants.ONE);
		//后面需要改为根据用户的角色Id查询
		List<Permission> list = listByPid(Integer.valueOf(Constants.ZERO), status);//查询一级菜单
		if (list.size() > 0) {
			for (Permission permission : list) {
				List<Permission> childrens = listByPid(permission.getId(), status);//二级菜单
				if (childrens.size() > 0) {
					for (Permission children : childrens) {
						List<Permission> btns = listByPid(children.getId(), status);//三级菜单{按钮}
						children.setChildren(btns);//二级菜单中保存按钮
						children.setList(btns);
					}
				}
				permission.setChildren(childrens);//一级菜单中保存二级菜单
				permission.setList(childrens);
			}
		}
		return list;
	}*/

	/**
	 * 查询全部，为了配合前端框架，没有嵌套查询
	 * @return
	 */
	public List<Permission> listAll() {
		return permissionMapper.listAll();
	}


	private List<Permission> listByPid(Integer pid, List<Integer> status) {
		/*Example example = new Example(Permission.class);
		Example.Criteria criteria = example.createCriteria();
		//封装查询条件
		criteria.andEqualTo("parentId", pid);
		criteria.andIn("status", status);
		example.setOrderByClause("sort");//根据sort排序
		return permissionMapper.selectByExample(example);*/
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("parentId",pid);
		params.put("status",status);
		return permissionMapper.listByPid(params);
	}


	/**
	 * 根据菜单ID删除菜单-角色关系表
	 * @param permissionId
	 */
	private void deleteRolePermByPermId(Integer permissionId) {
		Example example = new Example(Permission.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("permission_id", permissionId);
		rolePermissionMapper.deleteByPrimaryKey(example);
	}



}
